<style scoped>
.red {
  color: #EE0A24;
}

.grey {
  color: #bbb;
}

.container {
  width: 100%;
  height: 100%;
  background: #fff;
}
</style>

<template>
  <div class="container">
    <van-nav-bar @click-left="back">
      <van-icon size="20" name="arrow-left" slot="left" color="rgb(150, 151, 153)"/>
      <p slot="title" style="font-size: 14px">登录/注册</p>
    </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="username" label="手机号" placeholder="请输入手机号" autocomplete="off"/>
      <van-field v-model="password" type="password" name="password" label="验证码" placeholder="请输入验证码" autocomplete="off">
        <template #button>
          <van-button native-type="button" size="small" color="#fff" @click="sendCode">
            <template v-slot:default><span class="red">发送验证码</span></template>
          </van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button round block color="#F70D00" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <van-row>
      <van-col span="12" style="text-align: left">
        <router-link to="/loginpwd" tag="div">
          <van-button size="small" color="#fff" style="margin-left: 10px">
            <template v-slot:default><span class="grey">账号密码登录</span></template>
          </van-button>
        </router-link>
      </van-col>
      <van-col span="12" style="text-align: right">
        <router-link to="/register" tag="div">
          <van-button size="small" color="#fff" style="margin-right: 10px">
            <template v-slot:default><span class="grey">手机快速注册</span></template>
          </van-button>
        </router-link>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import Vue from 'vue'
import {mapMutations} from "vuex"
import {NavBar, Col, Row, Form, Field, Button} from 'vant';
import {requestIsRegister, requestToCheckCheckcode, requestToGetCheckcode} from "../../api/user";

Vue.use(NavBar)
Vue.use(Col)
Vue.use(Row)
Vue.use(Form)
Vue.use(Field)
Vue.use(Button)

export default {
  name: "Login",
  data() {
    return {
      username: '',
      password: '',
    }
  },
  // 挂载
  mounted() {
    // setTimeout(() => {
    //   this.password = "cqg000813"
    // }, 2000)
  },
  methods: {
    ...mapMutations({
      changeActive: 'tabs/doActive'
    }),
    onSubmit(e) {
      if (!e.username) {
        this.$toast({
          message: '手机号为空',
          type: 'fail'
        })
      } else if (!e.password) {
        this.$toast({
          message: '验证码为空',
          type: 'fail'
        })
      } else {
        // 判断用户是否注册
        requestIsRegister({tel: e.username}).then(res => {
          this.userState(res.code)
        })
      }
    },
    // 根据不同的用户状态进行下一步操作
    userState(code) {
      if (code == 200) {
        this.registerFaster()

      } else {
        this.login()
      }
    },
    // 快速注册
    registerFaster() {
      console.log("快速注册")
      requestToCheckCheckcode({tel: this.username, telcode: this.password}).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$dialog.confirm({
            title: '温馨提示',
            message: '你还没有注册，是否现在注册？'
          }).then(() => {
            this.$router.push('/password/' + this.username)
          }).catch(() => {
            this.$toast({
              message: '取消注册',
              type: 'fail'
            })
          })
        } else {
          this.$notify("验证码错误")
        }
      })
    },
    // 验证码登录
    login() {
      console.log("验证码登录")
      requestToCheckCheckcode({tel: this.username, telcode: this.password}).then(res => {
        if (res.code == 200) {
          this.$router.push('/pwd/' + this.username)
        } else {
          this.$notify('验证码错误')
        }
      })
    },
    // 发送验证码
    sendCode() {
      if(!this.username){
        this.$notify('手机号不得为空！')
        return;
      }
      requestToGetCheckcode({tel: this.username}).then(res => {
        console.log(res)
        this.password = res.data
      })
    },
    back() {
      this.$router.push('/')
      this.changeActive(0)
    },
  }
}
</script>